package com.cloudcentral.websocket.entity;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.Getter;

import java.util.Calendar;

/**
 * Create By Administrator
 * 2023-12-03 17:03
 */
@Data
@Schema(description = "Websocket消息结果")
public class WSMessage<T> {
  @Schema(description = "消息类型")
  private final String type;
  @Schema(description = "内容")
  private final T data;
  @Schema(description = "成功")
  private final boolean success;
  @Schema(description = "时间戳")
  private final long timestamp;

  public WSMessage(WSMessageType type, T data, boolean success) {
    this.type = type.getType();
    this.data = data;
    this.success = success && !type.equals(WSMessageType.ERROR);
    this.timestamp = Calendar.getInstance().getTime().getTime();
  }

  public static <T> WSMessage<T> heart() {
    return new WSMessage<>(WSMessageType.HEART, null, true);
  }

  public static <T> WSMessage<T> ok(WSMessageType type) {
    return new WSMessage<>(type, null, true);
  }

  public static <T> WSMessage<T> ok(WSMessageType type, T data) {
    return new WSMessage<>(type, data, true);
  }

  public static <T> WSMessage<T> fail(WSMessageType type) {
    return new WSMessage<>(type, null, false);
  }

  public static <T> WSMessage<T> fail(WSMessageType type, T data) {
    return new WSMessage<>(type, data, false);
  }

  public static <T> WSMessage<T> error() {
    return new WSMessage<>(WSMessageType.ERROR, null, false);
  }

  public static <T> WSMessage<T> error(T data) {
    return new WSMessage<>(WSMessageType.ERROR, data, false);
  }

  @Getter
  @Schema(description = "消息类型")
  public enum WSMessageType {
    ERROR("错误消息", "ERROR"),
    HEART("心跳消息", "HEART"),
    REGISTER("台站注册", "REGISTER"),
    PCM("PCM数据流", "PCM"),
    START_DETECT("启动探测", "START_DETECT"),
    STOP_DETECT("停止探测", "STOP_DETECT"),
    RADIO_STATUS("台站状态", "RADIO_STATUS"),
    DETECT_DATA("探测数据", "DETECT_DATA"),
    DETECT_RUNNING("探测中的任务", "DETECT_RUNNING");

    @Schema(description = "类型名称")
    private final String name;
    @Schema(description = "类型代码")
    private final String type;

    WSMessageType(String name, String type) {
      this.name = name;
      this.type = type;
    }
  }
}
